package com.tuanzi.blog.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tuanzi.blog.NotFoundException;
import com.tuanzi.blog.dao.UserMapper;
import com.tuanzi.blog.model.BlogWithBLOBs;
import com.tuanzi.blog.model.Tag;
import com.tuanzi.blog.model.Type;
import com.tuanzi.blog.service.BlogService;
import com.tuanzi.blog.service.TagService;
import com.tuanzi.blog.service.TypeService;
import com.tuanzi.blog.service.UserService;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 雄关漫道真如铁，而今迈过从头越
 **/

@Controller
public class indexController {

    @Resource
    private BlogService blogService;

    @Resource
    private TypeService typeService;

    @Resource
    private TagService tagService;

    @Resource
    private UserService userService;

    //访问进到博客主页
    @GetMapping("/")
    public String firstPage(){
        return "firstPage";
    }

    @GetMapping("/index")
    public String index(@PageableDefault(page = 1,size = 6,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable, BlogWithBLOBs blog, Model model){

        PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize() );
        List<BlogWithBLOBs> list = blogService.getAllBlogs();
        PageInfo<BlogWithBLOBs> pageInfo = new PageInfo<>(list);
        List<Type> allTypes = typeService.getAllTypes();
        List<Type> types = new ArrayList<>();int i = 0;
        for (Type allType : allTypes) {
            if (i == 6){
                break;
            }
            types.add(allType);
            i++;
        }
        model.addAttribute("page",pageInfo);
        model.addAttribute("types",types);
        model.addAttribute("tags",tagService.getAllTags());
        model.addAttribute("blogs",blogService.getTimeBlogs(6));

        return "index";
    }


    @PostMapping("/search")
    public String search(@PageableDefault(page = 1,size = 10000,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                         BlogWithBLOBs blog,
                         Model model,
                         @RequestParam("query") String query){
        PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize() );
        List<BlogWithBLOBs> list = blogService.searchBlogByTitleOrContent(query);
        PageInfo<BlogWithBLOBs> pageInfo = new PageInfo<>(list);
        model.addAttribute("page",pageInfo);
        model.addAttribute("user",userService.checkUser("tuanzi","521lmf1314"));
        model.addAttribute("query",query);
        return "search";
    }

    @GetMapping("/blog/{id}")
    public String blog(@PathVariable("id") Long id,Model model){

        BlogWithBLOBs blog = blogService.getBlog(id);
        blog.setType(typeService.getType(blog.getTypeId()));
        model.addAttribute("blog",blog);
        return "blog";
    }


}
